//
//  CreateRoadShowViewController.m
//  BigToBig
//
//  Created by yi liu on 16/8/2.
//  Copyright © 2016年 cn.cloudtp. All rights reserved.
//

#import "CreateRoadShowViewController.h"
#import "SZCalendarPicker.h"
#import "CreateRoadShowCell.h"
#import "TimePickerView.h"
#import "UnUseController.h"

@interface CreateRoadShowViewController ()
<
    UITableViewDelegate,
    UITableViewDataSource,
    TimePickerViewDelegate,
    UnUseControllerDelegate
>

@property (nonatomic , strong)UITableView *mainTableView;

@property (nonatomic , strong)NSArray *contentArray;

@property (nonatomic , strong)NSMutableDictionary *contentDic;

@end

@implementation CreateRoadShowViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    UILabel *titleLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
    titleLab.text = @"创建路演";
    titleLab.textColor = [UIColor whiteColor];
    [self setBackBar];
    titleLab.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLab;
    
    self.contentArray = @[@"路演日期",@"路演时间",@"路演时长",@"选择秀劵"];
    self.contentDic = [NSMutableDictionary new];
    [self createViews];

}
#pragma mark 导航栏左右Button
- (void)setBackBar{
    
    BackButton *backBtn=[BackButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 44, 44);
    [backBtn addTarget:self action:@selector(backController:) forControlEvents:UIControlEventTouchUpInside];
    [backBtn setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    [backBtn.imageView setContentMode:UIViewContentModeLeft];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backBtn];
    [self.navigationItem setHidesBackButton:YES animated:YES];
    
    BackButton *saveBtn=[BackButton buttonWithType:UIButtonTypeCustom];
    saveBtn.frame = CGRectMake(kScreenWidth - 44,0 , 44, 44);
    [saveBtn addTarget:self action:@selector(SaveMessage) forControlEvents:UIControlEventTouchUpInside];
    [saveBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [saveBtn setTitle:@"提交" forState:UIControlStateNormal];
    [saveBtn.imageView setContentMode:UIViewContentModeLeft];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:saveBtn];
    [self.navigationItem setHidesBackButton:YES animated:YES];

    
}
- (void)SaveMessage{
    
//    
//    NSUserDefaults* userID = [NSUserDefaults standardUserDefaults];
//    NSString *uid = [userID objectForKey:@"userID"];
//    NSDictionary *params = @{@"id":uid,@"company_name":compantyName.text,@"job_title":JobName.text,@"wechat":wechatNub.text,@"city":adressName.text};
//    
//    [HttpTool postWithURL:@"http://61.135.176.43:6080/bigtobig/user/update_by_id?" params:params success:^(id json) {
//        
//        NSString *returnCode = [NSString stringWithFormat:@"%@",json[@"return_code"]];
//        if([returnCode isEqualToString:@"0"]){
//            
//            [UIView animateWithDuration:2.0 animations:^{
//                
//                MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
//                hud.mode = MBProgressHUDModeText;
//                hud.labelText = @"保存成功";
//                [hud hide:YES afterDelay:2];
//                
//                
//                NSUserDefaults* jobDefaults = [NSUserDefaults standardUserDefaults];
//                [jobDefaults setObject:JobName.text forKey:@"jobName"];
//                [jobDefaults synchronize];
//                
//                NSUserDefaults* weChatDefaults = [NSUserDefaults standardUserDefaults];
//                [weChatDefaults setObject:wechatNub.text forKey:@"weChat"];
//                [weChatDefaults synchronize];
//                
//                NSUserDefaults* companyDefaults = [NSUserDefaults standardUserDefaults];
//                [companyDefaults setObject:compantyName.text forKey:@"company"];
//                [companyDefaults synchronize];
//                
//                NSUserDefaults* adressDefaults = [NSUserDefaults standardUserDefaults];
//                [adressDefaults setObject:adressName.text forKey:@"adress"];
//                [adressDefaults synchronize];
//                
//                
//                
//            } completion:^(BOOL finished) {
//                
//                [NSThread sleepForTimeInterval:1.0];
//                
//                NSMutableDictionary *dict = [NSMutableDictionary dictionary];
//                [dict setObject:self.savedImage forKey:@"saveImage"];
//                NSNotificationCenter * notificationCenter = [ NSNotificationCenter defaultCenter];
//                [notificationCenter postNotificationName:@"AnyNotification" object:nil userInfo:dict];
//                [self.navigationController popViewControllerAnimated:YES];
//                
//            }];
//            
//            
//        }
//        
//        
//    } failure:^(NSError *error) {
//        
//        dispatch_async(dispatch_get_main_queue(), ^{
//            
//            [UIView animateWithDuration:2.0 animations:^{
//                
//                MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
//                hud.mode = MBProgressHUDModeText;
//                hud.labelText = @"保存失败";
//                [hud hide:YES afterDelay:2];
//                
//            } completion:^(BOOL finished) {
//                
//                [NSThread sleepForTimeInterval:1.0];
//                
//                NSMutableDictionary *dict = [NSMutableDictionary dictionary];
//                [dict setObject:self.savedImage forKey:@"saveImage"];
//                NSNotificationCenter * notificationCenter = [ NSNotificationCenter defaultCenter];
//                [notificationCenter postNotificationName:@"AnyNotification" object:nil userInfo:dict];
//                [self.navigationController popViewControllerAnimated:YES];
//                
//            }];
//            
//            
//        });
//        
//    }];
    
    
    
}

- (void)createViews{
    // 创建mainTableView
    self.mainTableView.backgroundColor = [UIColor whiteColor];
    self.mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 84 - 20)];
    [self.view addSubview:self.mainTableView];
    self.mainTableView.delegate = self;
    self.mainTableView.dataSource = self;
    [self.mainTableView registerClass:[CreateRoadShowCell class] forCellReuseIdentifier:NSStringFromClass([CreateRoadShowCell class])];
    self.mainTableView.separatorStyle = NO;
    self.mainTableView.bounces = NO;
}

#pragma mark - mainTableview 代理方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CreateRoadShowCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([CreateRoadShowCell class]) forIndexPath:indexPath];
    cell.titleLabel.text = self.contentArray[indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    if (indexPath.row == 0) {
        NSString *date = [self.contentDic objectForKey:@"createRoadShow-date"];
        cell.messageLabel.text = date;
    }
    if (indexPath.row == 1) {
        if ([self.contentDic objectForKey:@"hour"] != NULL) {
            NSString *time = [NSString stringWithFormat:@"%@ : %@",[self.contentDic objectForKey:@"hour"],[self.contentDic objectForKey:@"minute"]];
            cell.messageLabel.text = time;

        }
    }
    if (indexPath.row == 2) {
        if ([self.contentDic objectForKey:@"duration"] != NULL) {
            NSString *time = [NSString stringWithFormat:@"%@",[self.contentDic objectForKey:@"duration"]];
            cell.messageLabel.text = time;
            
        }
    }
    if (indexPath.row == 3) {
        if ([self.contentDic objectForKey:@"xiuquanID"] != NULL) {
            NSString *xiuquanID = [NSString stringWithFormat:@"路演秀劵ID: %@",[self.contentDic objectForKey:@"xiuquanID"]];
            cell.messageLabel.text = xiuquanID;
            cell.messageLabel.font = [UIFont systemFontOfSize:14];
            
        }
    }

    
    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        SZCalendarPicker *calendarPicker = [SZCalendarPicker showOnView:self.view];
        calendarPicker.today = [NSDate date];
        calendarPicker.date = calendarPicker.today;
        calendarPicker.frame = CGRectMake(0, 100, self.view.frame.size.width, 352);
        calendarPicker.calendarBlock = ^(NSInteger day, NSInteger month, NSInteger year){
            NSString *date = [NSString stringWithFormat:@"%ld-%ld-%ld", year,month,day];
            [self.contentDic setObject:date forKey:@"createRoadShow-date"];
            [self.mainTableView reloadData];
        };

    }
    if (indexPath.row == 1) {
        TimePickerView *timePickerView = [[TimePickerView alloc]initWithFrame:self.view.frame];
        [self.view addSubview:timePickerView];
        timePickerView.timePickerDelegate = self;
    }
    if (indexPath.row == 2){
        TimePickerView *timePickerView = [[TimePickerView alloc]initWithFrame:self.view.frame];
        timePickerView.isDuration = YES;
        [self.view addSubview:timePickerView];
        timePickerView.timePickerDelegate = self;
    }
    if (indexPath.row == 3){
        UnUseController *ticketC = [[UnUseController alloc]init];
        ticketC.unuseControllerDelegate =self;
        ticketC.isCreateRoadShowPushed = YES;
        [self.navigationController pushViewController:ticketC animated:YES];
    }
}

- (void)refreshXiuJuanCellWithID:(NSString *)ticketID{
    [self.contentDic setObject:ticketID forKey:@"xiuquanID"];
    [self.mainTableView reloadData];
}

- (void)refreshRoadShowDurationWithDuration:(NSInteger)duration{
    [self.contentDic setObject:[NSString stringWithFormat:@"%ld 分钟",duration] forKey:@"duration"];
    [self.mainTableView reloadData];
}

- (void)refreshTableViewWithHour:(NSInteger)hour Minute:(NSInteger)minute{
    [self.contentDic setObject:[NSString stringWithFormat:@"%ld",hour] forKey:@"hour"];
    [self.contentDic setObject:[NSString stringWithFormat:@"%ld",minute] forKey:@"minute"];
    [self.mainTableView reloadData];
}

- (void)backController:(UIButton *)button{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
